{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## First Missing Positive Integer\n",
    "\n",
    "[原题](https://mp.weixin.qq.com/s/YB78xWSJABHh15fWDqt0ug)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question\n",
    "\n",
    "You are given an array of integers. Return the smallest positive integer that is not present in the array. The array may contain duplicate entries.\n",
    "\n",
    "For example, the input `[3, 4, -1, 1]` should return `2` because it is the smallest positive integer that doesn't exist in the array.\n",
    "\n",
    "**Requirements: Your solution should run in linear time and use constant space.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def first_missing_positive(nums):\n",
    "    k = 1\n",
    "    while k in nums:\n",
    "        k += 1\n",
    "    return k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "print(first_missing_positive([3, 4, -1, 1]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Notes\n",
    "\n",
    "The keyword `in` makes the list became a hash table. We just use this hash table, begin from 1, find out if our value doesn't exist."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
